package com.lagou.finger

class Computer {
  var name:String = _;
  var socre:Int = _;

  def showFist(choose:Int): Unit ={
    choose match {
      case 1 => println(s"${name}出拳！");println(s"${name}出拳:剪刀")
      case 2 => println(s"${name}出拳！");println(s"${name}出拳:石头")
      case 3 => println(s"${name}出拳！");println(s"${name}出拳:布")
    }
  }
}

package com.lagou.finger

class User {
  var name:String = _;
  var socre:Int = _;

  def showFist(choose:Int): Unit ={
    choose match {
      case 1 => println("你出拳:剪刀")
      case 2 => println("你出拳:石头")
      case 3 => println("你出拳:布")
      case _ => println("输入不符合规范，默认出布！")
    }
  }
}

package com.lagou.finger

class Game {

//  游玩次数
  var num:Int = _;
//  用户胜利次数
  var uWinNum:Int = _;
//  用户和局次数
  var uDrawNum:Int = _;
//  用户失败次数
  var uFailNum:Int = _;

  //  电脑胜利次数
  var cWinNum:Int = _;
  //  电脑失败次数
  var cFailNum:Int = _;

  val user = new User;
  val computer = new Computer;

  def init(): Unit ={
    user.name = "匿名";
    user.socre = 0;
    computer.name = "匿名";
    computer.socre = 0;
    num = 0;
    uWinNum = 0;
    uDrawNum = 0;
    uFailNum = 0;
    cWinNum = 0;
    cFailNum = 0;
  }

  def chooseComputer(index:Int): Unit = {
    index match {
      case 1 => computer.name = "刘备";
      case 2 => computer.name = "关羽";
      case 3 => computer.name = "张飞";
    }
  }

  def chooseUser(name:String): Unit ={
    user.name = name;
  }

  def begin(index:Int): Unit ={
    if (index == 1 || index == 2 || index == 3){
      num += 1;
      user.showFist(index);
//      随机生成1-3之间的数字
      val ranNum = scala.util.Random.nextInt(3)+1;
      computer.showFist(ranNum);
//      用户赢
      if (index == 1 && ranNum == 3 || index == 2 && ranNum == 1 || index == 3 && ranNum == 2 ){
        user.socre +=2;
        uWinNum += 1;
        cFailNum +=1;
        println("结果：恭喜，你赢了！")
//        电脑赢
      }else if(index == 1 && ranNum == 2 || index == 2 && ranNum == 3 || index == 3 && ranNum == 1 ){
        computer.socre +=2;
        cWinNum += 1;
        uFailNum += 1;
        println(s"结果： ${computer.name}赢！下次继续努力！")
//        平局
      }else{
        user.socre +=1;
        computer.socre +=1;
        uDrawNum +=1;
        println("结果：和局！下次继续努力！")
      }

    }
  }

  def calCount(): Unit ={
    println(s"${computer.name} VS ${user.name}")
    println(s"对战次数${num}次")
  }

  def result(): Unit ={
      println("姓名    分数    胜局    和局    负局");
      println(s"${user.name}    ${user.socre}      $uWinNum      $uDrawNum      $uFailNum")
      println(s"${computer.name}    ${computer.socre}      $cWinNum      $uDrawNum      $cFailNum")
  }
}

package com.lagou.finger

object PlayGame {
  def main(args: Array[String]): Unit = {
    var computer:Int = 0;
    var user:String = "";
    var uIndex:Int = 0;
    var flag = true;
    val g = new Game;
//    初始化游戏
    g.init();
    println("	   *********************");
    println("	       **猜拳，开始**");
    println("	   *********************");
    println("请输入玩家名称:");
    user = Console.readLine();
    g.chooseUser(user);
    println("请选择对战角色:(1.刘备 2.关羽 3.张飞)");
    computer = Console.readInt();
    if (computer == 1){
      println("你选择了与刘备对战");
    }else if (computer == 2){
      println("你选择了与关羽对战");
    }else if (computer == 3){
      println("你选择了与张飞对战");
    }else{
      println("输入错误，默认与刘备对战");
      computer = 1;
    }
    g.chooseComputer(computer);
    while (flag){
      println("请出拳！1.剪刀 2.石头 3.布");
      uIndex =  Console.readInt();
      if (uIndex < 1 || uIndex > 3){
        println("输入不符合规范，默认出布！")
        uIndex = 3;
      }
      g.begin(uIndex);
      println("是否开启下一轮(y/n)")
      val choose = Console.readLine();
      if (choose == "n"){
        flag = false;
        println("退出游戏！")
        println("---------------------------------------")
        g.calCount();
        g.result();
      }
    }
  }
}